import tool from 'src/util/tool'
import util from '_util/index';
import logic from '_logic';
import _ from 'lodash';

export default{
  ol_init(_this){
    _this.ol0 = !_this.ol;
    _this.ol1 = _this.ol;
  },
  setOl(_this,flag){
    let mid = _this.modelInfo.riliDayMid;
    switch (mid) {
      case 55:
        this.setProductOl(_this,flag);
        break;
      case 60:
        this.setWhprodOl(_this,flag);
        break;
    }
  },
  setProductOl(_this,flag){
    util.showConfirm(_this, ()=> {

      let vo = {product_ids:_this.product_id,ol:String(flag)};
      let url = tool.getApi(_this, 'Set', 'setProductOl');
      util.http.SR(_this, url, vo, '操作成功',()=>{
        this.set_ol(_this, flag);
      });
    });

  },
  set_ol(_this,flag){
    _this[`ol${flag}`] = true;
    let opposite=Number(!Boolean(flag));
    _this[`ol${opposite}`] = false;
  },
  setWhprodOl(_this,flag){
    util.showConfirm(_this, ()=> {

      let vo = {product_ids:_this.product_id,ol:String(flag)};
      let url = tool.getApi(_this, 'Set', 'setWhprodOl');
      util.http.SR(_this, url, vo, '操作成功',()=>{
        this.set_ol(_this, flag);
      });
    });

  },

  toEmit(_this){
    let out_product = {
      is_quantity_date: _this.is_quantity_date,
      quantity_calendar_list: _this.is_quantity_date ? JSON.stringify(_this.data) : null,
      only_quantity: _this.is_quantity_date ? null : JSON.stringify(_this.data_single),
      product_id: _this.product_id
    };
    let out_whprod = {
      is_quantity_date: _this.is_quantity_date,
      price_calendar: _this.is_quantity_date ? JSON.stringify(_this.data) : null,
      only_price: _this.is_quantity_date ? null : JSON.stringify(_this.data_single),
      product_id: _this.product_id
    };
    let mid = _this.modelInfo.riliDayMid;
    _this.outObj = mid == 55 ? out_product : out_whprod;
    _this.$emit('setVo', _this.field.name, JSON.stringify(_this.outObj));
  },
  getProductAttr(_this){
    let url = tool.getApi(_this, 'Editget', _this.modelInfo.getAttr);
    let param = {};
    param.product_id = _this.product_id;
    param.warehouse_product_id = _this.product_id;
    util.getDataCom(_this, url, param, (rs) => {
      _this.ols = rs.ols;
      _this.is_dates = rs.is_dates;
      _this.attr_data = rs.data_all;
      rs=rs.attr_names;
      if(rs&&Object.keys(rs).length>0) {

        for(let ind in rs) {
          _this.group_attr.push({
            val: ind,
            title: rs[ind]
          })
        }

      }

    })
  },
  editAttr(_this,g){
    util.store.commit(_this,'setModalOp', 'update');
    let vo = {
      attr_name: g.title,
      parent_id: _this.product_id==g.val?0:_this.product_id,
      product_id: g.val,
      warehouse_product_id: g.val
    };
    vo = _.merge(g,vo);

    logic.edit.showEditPop(_this, _this.modelInfo.attr_mid, ()=> {
      let newvo = util.store.get(_this, 'editVoModal');
      let url = tool.getApi(_this, _this.modelInfo.AttrModel, 'update');
      let param = newvo;

      util.http.SR(_this, url, param, '操作成功', (rs) => {
        let ind = _.findIndex(_this.group_attr, {val: g.val});
        if(ind!==-1){
          // let newvo = util.store.get(_this, 'editVoModal');
          let newobj = {
            val: g.val,
            title: newvo.attr_name
          };
          newobj = _.merge(newvo,newobj);
          _this.group_attr.splice(ind,1,newobj);
        }
      });
    },vo);
  },
  addAttr(_this){
    util.store.commit(_this,'setModalOp', 'insert');
    logic.edit.showEditPop(_this, _this.modelInfo.attr_mid, ()=> {
      let vo = util.store.get(_this, 'editVoModal');
      let url = tool.getApi(_this, _this.modelInfo.AttrModel, 'insert');
      let param = vo;
      param.product_id = _this.product_id
      util.http.SR(_this, url, param, '操作成功', (rs) => {
        let newvo = util.store.get(_this, 'editVoModal');
        _this.group_attr.push({
          val: rs.id,
          delivery_group_id: newvo.delivery_group_id,
          delivery_group_name: newvo.delivery_group_name,
          title: vo.attr_name
        })
      });

    });
  },


}
